package com.sduonline.isdapp.servlet;

import java.awt.List;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.ResponseWrapper;

import com.alibaba.fastjson.JSONObject;
import com.sduonline.isdapp.jdbc.CreateCategoryDao;
import com.sduonline.isdapp.jdbc.WebAdminDao;
import com.sduonline.isdapp.jdbc.WebDao;
import com.sduonline.isdapp.model.Web;
import com.sduonline.isdapp.model.WebAdmin;
import com.sduonline.isdapp.util.SHA1Utils;

public class CreateCategoryServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	private int id,web=0;
	private  String name,url, signature;
	private WebAdminDao wDao;
	private CreateCategoryDao cDao;
	long timestamp=0;
	public CreateCategoryServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
          doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	//JSONObject  jsonObject=JSONObject.parseObject(request.toString());
		PrintWriter respPrintWriter=response.getWriter();
		response.setCharacterEncoding("utf-8");
	try {
		
	web=Integer.parseInt(request.getParameter("web"));
	name=request.getParameter("name");
	url=request.getParameter("url");
	
	 timestamp=Long.parseLong(request.getParameter("timestamp"));
	signature=request.getParameter("signature");
	} catch (Exception e) {
		respPrintWriter.write("参数错误");
		return;
	}
	    
		if(web==0||name==null||url==null){
			respPrintWriter.write("有值为空");
			return;
		}
		  if(checkSignature(web, timestamp, signature)){
			  cDao=new CreateCategoryDao();
			  if(cDao.isExist(web, name))
				  respPrintWriter.write("该category已经存在");
			  else if(cDao.addCategory(web,name,url))
				  respPrintWriter.write("您的文章的ID为："+cDao.getMaxID());
			  else {
				respPrintWriter.write("未创建成功");
			}}
			  else {
					respPrintWriter.write("签名错误");
			  
			  }
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}
	public boolean checkSignature(int web,long timestamp,String signature){
		wDao=new WebAdminDao();
		WebAdmin w=wDao.getByWebId(web);
		String password=w.getPassword();
		String sign = SHA1Utils.SHA1(password+ timestamp);// username和timestamp拼接，sha1加密
		if (sign.equals(signature)) {
			return true;
		} else {
			return false;
		}
	}

}
